package com.example.study.testsynchronized;

/**
 * 测试同步
 *
 * @author hjh
 * @create 2019-08-19 23:40
 **/
class TestSyncThread02 implements Runnable{
    private static int count;

    String lock = "";

    public TestSyncThread02() {
        count = 0;
    }
    @Override
    public void run() {
        synchronized(lock) {
            for (int i = 0; i < 5; i++) {
                try {
                    System.out.println(Thread.currentThread().getName() + ":" + (count++));
                    Thread.sleep(100);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
    }
    public static void main(String args[]){
//test01
//        TestSyncThread02 s1 = new TestSyncThread02();
//        TestSyncThread02 s2 = new TestSyncThread02();
//        Thread t1 = new Thread(s1);
//        Thread t2 = new Thread(s2);
//test02 用同一个实例对象
        TestSyncThread02 s = new TestSyncThread02();
             Thread t1 = new Thread(s);
             Thread t2 = new Thread(s);
//
        t1.start();
        t2.start();
    }
}
